use eframe::epaint::{Color32, FontId};
use egui::{ComboBox, RichText, Ui};

pub(crate) struct LComboBox {
    id: &'static str,
    title: &'static str,
    ltype: LComboBoxType,
    lwidth: f32,
    title_size: f32,
    title_color: Color32,
    sel_size: f32,
    sel_color: Color32,
    item_size: f32,
    item_color: Color32,
    rang_start: usize,
    rang_end: usize,
    items: Vec<String>,
}

impl LComboBox {
    pub(crate) fn new(sid: &'static str) -> Self {
        LComboBox {
            id: sid,
            title: "",
            ltype: LComboBoxType::Number,
            lwidth: 80.0,
            title_size: 14.0,
            title_color: Color32::GRAY,
            sel_size: 15.0,
            sel_color: Color32::BLACK,
            item_size: 14.0,
            item_color: Color32::GRAY,
            rang_start: 0,
            rang_end: 2000,
            items: vec![],
        }
    }

    pub(crate) fn title(
        mut self,
        title: &'static str,
        title_size: f32,
        title_color: Color32,
    ) -> Self {
        self.title = title;
        self.title_size = title_size;
        self.title_color = title_color;
        self
    }
    pub(crate) fn title_name(mut self, title: &'static str) -> Self {
        self.title = title;
        self
    }

    pub(crate) fn ltype(mut self, ltype: LComboBoxType) -> Self {
        self.ltype = ltype;
        self
    }

    pub(crate) fn sel_text(mut self, sel_size: f32, sel_color: Color32) -> Self {
        self.sel_size = sel_size;
        self.sel_color = sel_color;
        self
    }

    pub(crate) fn item_text(mut self, item_size: f32, item_color: Color32) -> Self {
        self.item_size = item_size;
        self.item_color = item_color;
        self
    }
    pub(crate) fn range_number(mut self, rang_start: usize, rang_end: usize) -> Self {
        self.rang_start = rang_start;
        self.rang_end = rang_end;
        self
    }

    pub(crate) fn items(mut self, items: Vec<String>) -> Self {
        self.items = items;
        self
    }

    pub(crate) fn build(&self, ui: &mut Ui, sel_mut: &mut usize) {
        if !self.title.is_empty() {
            ui.label(
                RichText::new(self.title)
                    .font(FontId::monospace(self.title_size))
                    .color(self.title_color),
            );
        }
        let rtext = match self.ltype {
            LComboBoxType::Number => {
                let sel = &*sel_mut;
                RichText::new(sel.to_string())
            }
            LComboBoxType::Vec => {
                let sel_p = *sel_mut;
                RichText::new(self.items[sel_p].clone())
            }
        };
        let rtv = rtext
            .font(FontId::monospace(self.sel_size))
            .color(self.sel_color);
        ComboBox::new(self.id, "")
            .selected_text(rtv)
            .width(self.lwidth)
            .show_ui(ui, |ui| {
                // 调整此 UI 上下文的字体
                match self.ltype {
                    LComboBoxType::Number => {
                        for item in self.rang_start..self.rang_end {
                            let rtv = RichText::new(item.to_string())
                                .font(FontId::monospace(self.item_size))
                                .color(self.item_color);
                            ui.selectable_value(sel_mut, item, rtv);
                        }
                    }
                    LComboBoxType::Vec => {
                        for item in 0..self.items.len() {
                            let rtv = RichText::new(&self.items[item])
                                .font(FontId::monospace(self.item_size))
                                .color(self.item_color);
                            ui.selectable_value(sel_mut, item, rtv);
                        }
                    }
                }
            });
    }
}

pub(crate) enum LComboBoxType {
    Number,
    Vec,
}
